package com.gwclh.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gwclh.personnel.domain.PageDomain;
import com.gwclh.personnel.entity.Dismission;
import com.gwclh.personnel.entity.Employee;
import com.gwclh.personnel.entity.Department;
import com.gwclh.personnel.entity.Dismission;
import com.gwclh.personnel.mapper.DepartmentMapper;
import com.gwclh.personnel.mapper.DismissionMapper;
import com.gwclh.personnel.mapper.EmployeeMapper;
import com.gwclh.personnel.service.IDismissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwclh.personnel.vo.DismissionVo;
import com.gwclh.personnel.vo.employeevo.EmployeeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工离职记录表 服务实现类
 * </p>
 *
 * @author gzhhh
 * @since 2020-07-24
 */
@Service
public class DismissionServiceImpl extends ServiceImpl<DismissionMapper, Dismission> implements IDismissionService {

    @Autowired
    private DismissionMapper dismissionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public IPage<Map<String, Object>> selectEmp() {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> mapIPage = dismissionMapper.getAll(mapPage, new QueryWrapper());
        return mapIPage;
    }

    @Override
    public IPage<Map<String, Object>> selectEmpBySearch(String deptName, Integer empId, String empName) {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> empBySearch = dismissionMapper
                .getEmpBySearch(mapPage, new QueryWrapper(), deptName, empId, empName);
        return empBySearch;
    }

    @Override
    public Integer quit(Dismission dismission) {
        UpdateWrapper<Employee> update = Wrappers.update();
        update.eq("id",dismission.getEmpId());
        int delete = employeeMapper.delete(update);

        int insert = dismissionMapper.insert(dismission);
        return insert;
    }

    @Override
    public IPage<Map<String, Object>> selectQuit() {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> mapIPage = dismissionMapper.selectQuit(mapPage, new QueryWrapper());
        return mapIPage;
    }

    @Override
    public IPage<Map<String, Object>> selectQuitBySearch(Integer empId, String deptName, String staName) {
        Page<Map<String, Object>> mapPage = new Page<>(1,5);
        IPage<Map<String, Object>> mapIPage = dismissionMapper.selectQuitBySearch(mapPage, new QueryWrapper(), empId, deptName, staName);
        return mapIPage;
    }


    // @Autowired
    // DismissionMapper dismissionMapper;

    // @Autowired
    // DepartmentMapper departmentMapper;

    @Override
    public Map<String, Object> statisticsDismissionEmpMonth() {
        return dismissionMapper.statisticsDismissionEmpMonth();
    }

    @Override
    public List<Map<String,Object>> getDismissionDataByPid(Integer pId) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Department> departments = departmentMapper.selectList(new QueryWrapper<Department>().eq("p_id", pId));
        departments.forEach(department -> {
            Map<String, Object> dismissionDataByDeptId =
                    dismissionMapper.selectDimissionDataByDeptId(department.getId());
            dismissionDataByDeptId.put("name", department.getDeptName());
//            System.out.println(dismissionDataByDeptId);
            result.add(dismissionDataByDeptId);
        });
        return result;
    }

    @Override
    public Map<String, Object> pageDismissions(PageDomain pageDomain, Dismission dismission) {
        List<DismissionVo> resultList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        Page<Dismission> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        page.addOrder(pageDomain.getOrderItemBy());

        this.page(page);

        page.getRecords().forEach(item -> {
            DismissionVo dismissionVo = new DismissionVo();
            BeanUtils.copyProperties(item, dismissionVo);
            dismissionVo.setEmployee(employeeMapper.selectById(item.getEmpId()));
            resultList.add(dismissionVo);
        });
        resultMap.put("total", page.getTotal());
        resultMap.put("dismissions", resultList);
        return resultMap;
    }
}
